package com.example.service.impl;

import com.example.domain.Student;
import com.example.persistence.PersistenceService;
import com.example.service.ServiceException;
import com.example.service.StudentService;
import com.example.service.loader.PersistenceServiceLoader;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @author 01
 * @date 2019-10-05
 **/
@Slf4j
public class StudentServiceImpl implements StudentService {

    private PersistenceService persistenceService =
            PersistenceServiceLoader.persistenceService;

    @Override
    public List<Student> list() throws ServiceException {
        return persistenceService.list(Student.class);
    }

    @Override
    public Optional<Student> getStudent(Long id) throws ServiceException {
        return persistenceService.get(Student.class, id);
    }

    @Override
    public void addStudent(Student student) throws ServiceException {
        persistenceService.save(student);
    }

    @Override
    public void add(Long id, String name, String group) throws ServiceException {
        addStudent(new Student(id, name, group));
    }

    @Override
    public void updateStudent(Student student) throws ServiceException {
        persistenceService.save(student);
    }

    @Override
    public void deleteStudent(Student student) throws ServiceException {
        persistenceService.delete(student.getClass(), student.getId());
    }
}
